<?php
  /**
   * iJBlocksControl
   *
   * The iJBloÑks class provides bloÑks  menegment
   *
   * @copyright Copyright (c) 2011, iJEngine Progect
   */
    class BlocksControl extends iJBlock implements iJSingleton
    {
        /**
         *
         * class construct
         *
         *
         * @access public
         */
        public function __construct() {
        }
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          $iJLanguages = iJFactory::singleton('iJLanguages');
          $iJGroups    = iJFactory::singleton('iJGroups');
          if(isset($_POST['create'])) {
             //TODO Когда создаётся новый блок нет проверки что такой блок или файлы которые пытаюсть создать уже существуют
             $this->createBlock($_POST);
          } 
          
          if(isset($_POST['edit'])) {
             $this->editBlock($_POST);
          } 
          
          
          
          if(isset($_REQUEST['delID'])) {
            $this->deleteBlock($_REQUEST['delID']);
          }
          
         
          $page   = isset($_REQUEST['page'])                                  ? $_REQUEST['page']   : 1;
          $search = isset($_REQUEST['search']) && !empty($_REQUEST['search']) ? $_REQUEST['search'] : '';
          $group  = isset($_REQUEST['group']) ? $_REQUEST['group']  : DEFAULT_GROUP_ID_TO_VIEW;
          
          $user_groups = $iJGroups->getGroups();
          
          $blocksList = $this->getBlocksList($page, $search, $group);
          $blocks      = $blocksList[0];
          $Qblocks     = $blocksList[1];


          
          $this->addToken('blocks',             $blocks);         
          $this->addToken('search',             $search);
          $this->addToken('page',               $page);
          $this->addToken('groupId',            $group);
          $this->addToken('groups_icon',        iJHrefLink::Icon('group')); 
          $this->addToken('delete_icon',        iJHrefLink::Icon('trash', '16x16'));
          $this->addToken('edit_icon',          iJHrefLink::Icon('edit', '16x16'));
          $this->addToken('language_editor',    iJHrefLink::Icon('blue_flag', '16x16'));
          $this->addToken('form_action',        iJHrefLink::linkToLocalPage('admin_blocks') . '?' . iJGetAllGetParams(array('delID')));           
          $this->addToken('curLanguage',        $iJLanguages->getLanguage());
          $this->addToken('languages',          $iJLanguages->getLanguagesExeptCurrent());  
          $this->addToken('user_groups',        $user_groups);
          $get = array();
          $get['group']  = $group;
          $get['search'] = $search;
          $this->addToken('total_items',    $Qblocks->getSize());
          $this->addToken('pages_count',    $Qblocks->getPagesCount());
          $this->addToken('prev_link',      $Qblocks->getPerviousLink($page, 'admin_blocks', $get));
          $this->addToken('next_link',      $Qblocks->getNextLink($page, 'admin_blocks', $get));
             
          return parent::execute();
        }
        
        
       
        
        
        
        
    }
?>